package com.cloudfun.campusshare.service.impl;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.DeviceStatus;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.BasePayDTO;
import com.cloudfun.campusshare.entity.OrderEntity;
import com.cloudfun.campusshare.entity.OrderRefundEntity;
import com.cloudfun.campusshare.repo.DeviceEntityRepo;
import com.cloudfun.campusshare.service.DeviceOrderService;
import com.cloudfun.campusshare.service.PayService;
import com.cloudfun.campusshare.service.WeChatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * Created by Huqin on 2020/8/11
 */
@Slf4j
@Service
public class FreeOrderPayServiceImpl implements PayService {

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private DeviceOrderService deviceOrderService;

    @Autowired
    private DeviceEntityRepo deviceEntityRepo;

    @Override
    public boolean support(OrderEntity orderEntity) {
        // 支持0元订单
        return NumberUtils.INTEGER_ZERO.equals(orderEntity.getSettlementAmount());
    }

    @Override
    public BasePayDTO payOrder(OrderEntity orderEntity) {
        // 如果是0元 直接已支付并启动
        BasePayDTO payDTO = new BasePayDTO();
        payDTO.setOrderId(orderEntity.getId());
        payDTO.setOrderNo(orderEntity.getOrderNo());
        deviceOrderService.handlePaySuccess(orderEntity, LocalDateTime.now());
        return payDTO;
    }

    /**
     * 退款
     *
     * @param refundEntity
     */
    @Override
    public void refund(OrderRefundEntity refundEntity) {
        throw new BusinessException(Code.ERROR, "退款不支持退款");
    }
}
